#include<bits/stdc++.h>
using namespace std;
int n,t,m,aa[100005],a[100005],x[5],c[100005],k;
string s[1005];
int p(int dep){
    while (dep<=k){
        dep++;
        int s1,s2,f,turn;
        for (int i=1;i<=c[i];i++){
            if (a[i]>=0){
                if (f==0) s1=i,f=1;
                else{
                    if (s[dep][turn]=='0'){
                        if (a[s1]>=dep) a[s2]=-1;
                        else a[s1]=-1;
                    }else{
                        if (a[s2]>=dep) a[s1]=-1;
                        else a[s2]=-1;
                    }
                }
            }
        }
    }
    for (int i=1;i<=c[i];i++){
        if (a[i]>=0) return i;
    }
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    while ((1<<k)<n) k++;
    for (int i=1;i<=n;i++) cin>>aa[i];
    for (int j=1;j<=m;j++) cin>>c[j];
    for (int i=1;i<=k;i++){
        cin>>s[i];
    }
    cin>>t;
    while (t--){
        int ans=0;
        for (int i=0;i<4;i++){
            cin>>x[i];
        }
        for (int i=1;i<=n;i++){
            a[i]=aa[i]^x[i%4];
        }
        for (int i=1;i<=m;i++){
            k=0;
            while ((1<<k)<c[i])k++;
            ans=ans^(p(1)*i);
        }
        cout<<ans<<endl;
    }
    return 0;
}
